package service

import (
	"demo/internal/model"
	"demo/internal/repository"
	"errors"
)

type UserService struct {
	repo *repository.UserRepo
}

func NewUserService(repo *repository.UserRepo) *UserService {
	return &UserService{repo}
}

func (s *UserService) Register(u *model.User) error {
	return s.repo.Create(u)
}

func (s *UserService) Login(name, pwd string) (*model.User, error) {
	user, err := s.repo.FindByName(name)
	if err != nil {
		return nil, err
	}
	if user.Password != pwd {
		return nil, errors.New("password incorrect")
	}
	return user, nil
}

// ListUsers 分页查询用户列表
func (s *UserService) ListUsers(nickname string, page, size int) ([]*model.User, int64, error) {
	if page < 1 {
		page = 1
	}
	if size < 1 {
		size = 10
	}
	return s.repo.FindByNickname(nickname, page, size)
}
